package com.eksframework.commons.core.exception;

import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 自定义异常
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2019-12-06 14:55
 * @blame Team
 */
@Slf4j
public class EksException extends RuntimeException implements IException {

    private static final long serialVersionUID = -4682832496873078898L;

    private static final String DEFAULT_CODE = "500";
    private static final String DEFAULT_MESSAGE = "系统出错";

    /** 异常编码 */
    private final String code;

    private EksException(){
        super();
        this.code = DEFAULT_CODE;
    }

    public EksException(String message){
        super(message);
        this.code = DEFAULT_CODE;
    }

    public EksException(String code, String message){
        super(message);
        this.code = code;
    }

    public EksException(Throwable cause){
        super(cause);
        this.code = DEFAULT_CODE;
    }

    public EksException(String code, Throwable cause){
        super(cause);
        this.code = code;
    }

    public static EksException of(String message){
        return new EksException(message);
    }

    public static EksException of(String code, String message){
        return new EksException(code, message);
    }

    public static EksException of(Throwable cause){
        return new EksException(cause);
    }

    public static EksException of(String code, Throwable cause){
        return new EksException(code, cause);
    }

    /**
     ** 获取异常编码
     *
     * @return {@link String}
     */
    @Override
    public String getCode(){
        return code;
    }

    /**
     ** 获取异常信息
     *
     * @return {@link String}
     */
    @Override
    public String getMessage(){
        return super.getMessage() == null ? DEFAULT_MESSAGE : super.getMessage();
    }

    /**
     ** 获取异常堆栈
     *
     * @return {@link String}
     */
    @Override
    public String getTrace() {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        this.printStackTrace(pw);
        return sw.getBuffer().toString();
    }

    /**
     ** 打印异常堆栈日志
     */
    @Override
    public void logStackTrace(){
        log.error("", this.fillInStackTrace());
    }

    /**
     ** 打印异常堆栈日志
     *
     * @param msg 错误消息
     */
    @Override
    public void logStackTrace(String msg){
        if(log.isErrorEnabled()){
            log.error(msg, this.fillInStackTrace());
            return;
        }
        if(log.isWarnEnabled()){
            log.warn(msg, this.fillInStackTrace());
            return;
        }
        if(log.isInfoEnabled()){
            log.info(msg, this.fillInStackTrace());
            return;
        }
        if(log.isDebugEnabled()){
            log.debug(msg, this.fillInStackTrace());
            return;
        }
        if(log.isTraceEnabled()){
            log.trace(msg, this.fillInStackTrace());
            return;
        }
        this.printStackTrace();
    }
}
